#    Copyright 2008, 2009 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPGIS.
#    COOPGIS is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPGIS is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPGIS.  If not, see <http://www.gnu.org/licenses/>.


#************************BEGIN Add Include Files to Path************************
import sys
NeededPaths=['/opt/coopgis/djangoproject/include', '/opt/coopgis/djangoproject']
for path in NeededPaths:
    if(not path in sys.path):
        sys.path.append(path)
#************************END   Add Include Files to Path************************
import COOPGIS_Ajax, COOPGIS_FormMisc, COOPGIS_FormVegetationManager, COOPGIS_GPS, COOPGIS_Misc, COOPGIS_ReportLab, COOPGIS_String, COOPGIS_Update
import COOPGIS_FormGeography
import os, csv, shutil, datetime, time, reportlab, djangoproject.settings, random
from django.contrib.gis.db import models
from django.contrib.gis.geos import *
from django.http import HttpResponse
from django.contrib import admin
from django.template import Context, loader
import djangoproject.settings
import _mysql as mysql
from models import *
from django import forms


def MainMenu(request):
    import django.db
    ApplicationName=djangoproject.settings.COOPGISVegetationManagerApplicationName
    UserNameOrIPAddress=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=False, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    if(not applicationPermission):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/vegetationmanager_mainmenu.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))
    t = loader.get_template('/opt/coopgis/djangoproject/templates/vegetationmanager_mainmenu.html')
    c = Context({'LoginStatus' : LoginStatus})
    return HttpResponse(t.render(c))


def VegetationManagerCreateEditJob(request):
    ApplicationName=djangoproject.settings.COOPGISVegetationManagerApplicationName
    UserNameOrIPAddress=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=False, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    if(not applicationPermission):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/vegetationmanager_mainmenu.html')
        c = Context({'LoginStatus':LoginStatus})
        return HttpResponse(t.render(c))
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'CreateEditJob')
    SelectedVegetationManagerJobid=COOPGIS_FormVegetationManager.ProcessVegetationManagerCreateEditJobForm(form, UserNameOrIPAddress)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'CreateEditJob')
    form=COOPGIS_FormVegetationManager.PopulateVegetationManagerCreateEditJobForm(form, SelectedVegetationManagerJobid)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/vegetationmanager_createeditjob.html')
    c = Context({'form': form, 'LoginStatus':LoginStatus})
    return HttpResponse(t.render(c))


def VegetationManagerWorkJob(request):
    ApplicationName=djangoproject.settings.COOPGISVegetationManagerApplicationName
    UserNameOrIPAddress, DefaultMap=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=True, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'WorkJob', DefaultMap) 
    if(not applicationPermission): #Show view of Map and Form if no Permissions, do not perform any form processing
        MapFileName, LayerName=COOPGIS_Misc.GenerateDynamicMapFile(ApplicationName, UserNameOrIPAddress)
        OpenLayersZoom=COOPGIS_Ajax.GetOpenLayersZoom(None, None)
        t = loader.get_template('/opt/coopgis/djangoproject/templates/vegetationmanager_workjob.html')
        c = Context({'OpenLayersZoom': OpenLayersZoom, 'form': form,
                     'MapFileName': MapFileName, 'LayerName': LayerName, 'LoginStatus':LoginStatus})
        return HttpResponse(t.render(c))
    COOPGIS_FormGeography.GetAndSetDefaultMap(form, ApplicationName, UserNameOrIPAddress)
    SelectionStation, SelectionSpan, GPSStatus = COOPGIS_FormGeography.SetSelectionStationAndSpan(form)
    if SelectionStation:
        Zoom=23
    else:
        Zoom=15

    SelectedVegetationManagerJobid=COOPGIS_FormVegetationManager.ProcessVegetationManagerWorkJobForm(form, UserNameOrIPAddress)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'WorkJob', DefaultMap)
    form=COOPGIS_FormVegetationManager.PopulateVegetationManagerWorkJobForm(form, SelectedVegetationManagerJobid)
    MapFileName, LayerName=COOPGIS_Misc.GenerateDynamicMapFile(ApplicationName, UserNameOrIPAddress)

    OpenLayersZoom=COOPGIS_Ajax.GetOpenLayersZoom(SelectionStation, SelectionSpan, Zoom=Zoom)  
    OpenLayersJavaScriptHeading=COOPGIS_Ajax.GetOpenLayersJavaScriptHeading()
    OpenLayersJavaScript=COOPGIS_Ajax.GetOpenLayersJavaScript(OpenLayersZoom, MapFileName, LayerName, OpenLayersJavaScriptHeading, GeometryFeature = 'Polygon')
    t = loader.get_template('/opt/coopgis/djangoproject/templates/vegetationmanager_workjob.html')
    c = Context({'form': form, 'LoginStatus':LoginStatus,
                 'MapFileName': MapFileName,
                 'OpenLayersZoom': OpenLayersZoom,
                 'OpenLayersJavaScript': OpenLayersJavaScript,
                 'OpenLayersJavaScriptHeading':OpenLayersJavaScriptHeading})
    return HttpResponse(t.render(c))




def VegetationManagerDetails(request):
    ApplicationName=djangoproject.settings.COOPGISVegetationManagerApplicationName
    UserNameOrIPAddress, DefaultMap=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=True, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'Details', DefaultMap) 
    if(not applicationPermission): #Show view of Map and Form if no Permissions, do not perform any form processing
        t = loader.get_template('/opt/coopgis/djangoproject/templates/vegetationmanagerdetails.html')
        c = Context({'form': form, 'LoginStatus':LoginStatus})
        return HttpResponse(t.render(c))
    SpecialBuildFormInfo=COOPGIS_FormVegetationManager.SpecialProcessVegetationManagerDetailsForm(form)
    vegetationManagerJob=COOPGIS_FormVegetationManager.ProcessVegetationManagerDetailsFormJob(form, UserNameOrIPAddress)
    vegetationManagerCrew=COOPGIS_FormVegetationManager.ProcessVegetationManagerDetailsFormCrew(form, UserNameOrIPAddress)
    vegetationManagerJobStatus=COOPGIS_FormVegetationManager.ProcessVegetationManagerDetailsFormJobStatus(form, UserNameOrIPAddress)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'Details', DefaultMap, SpecialBuildFormInfo=SpecialBuildFormInfo) 
    form=COOPGIS_FormVegetationManager.PopulateVegetationManagerDetailsFormJob(form, vegetationManagerJob)
    form=COOPGIS_FormVegetationManager.PopulateVegetationManagerDetailsFormCrew(form, vegetationManagerCrew)
    form=COOPGIS_FormVegetationManager.PopulateVegetationManagerDetailsFormJobStatus(form, vegetationManagerJobStatus)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/vegetationmanagerdetails.html')
    c = Context({'form': form, 'LoginStatus':LoginStatus, })
    return HttpResponse(t.render(c))


